// pages/myCardEdit/myCardEdit.js
const app = getApp();
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1900; i <= new Date().getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeShow: false,
    refresh: true,
    nameIpt: false,
    industryIpt: false,
    jobIpt: false,
    addressIpt: false,
    phoneIpt: false,
    emailIpt: false,
    isMore: false,
    isBusinessCard: [],
    company:false,
    birthdayDefault:0,
    isShowBirthday:0,
    isShowBirthday1: 0,
    showBirthday:false,
    birthday:'',
    years: years,
    year: date.getFullYear(),
    months: months,
    month: 1,
    days: days,
    day: 1,
    value: [95, 0, 0],
    val:[95, 0, 0],
  },
  showType() {
    this.toggle("typeShow", true)
  },
  typeClose() {
    this.toggle("typeShow", false)
  },
  showbirthday(){
    this.toggle("showBirthday", true)
  },
  bindChange: function (e) {
    const val = e.detail.value
    if(this.data.years[val[0]] >= new Date().getFullYear()){
      var months1 = [];
      var days1 = [];
      for (let i = 1; i <= new Date().getMonth() + 1; i++) {
        months1.push(i)
      }
      for (let i = 1; i <= new Date().getDate(); i++) {
        days1.push(i)
      }
      this.toggle("months", months1);
      this.toggle("days", days1);
    }else{
      this.toggle("months", months);
      this.toggle("days", days);
    }
    this.toggle("val", val)
  },
  ischoose(){
    if(this.data.isShowBirthday1==0){
      this.toggle("isShowBirthday1", 1)
    }else{
      this.toggle("isShowBirthday1", 0)
    }
  },
  bircancel(){
    this.toggle("showBirthday", false)
    if (this.data.isShowBirthday1 != this.data.isShowBirthday) {
      this.toggle("isShowBirthday1", this.data.isShowBirthday)
    }
  },
  birsure(){
    const val =this.data.val
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      showBirthday:false,
      birthday: this.data.years[val[0]] + '-' + this.data.months[val[1]] + '-' +this.data.days[val[2]]+' 00:00:00'
    })
    if (this.data.isShowBirthday1 != this.data.isShowBirthday) {
      this.toggle("isShowBirthday", this.data.isShowBirthday1)
    }
  },
  chooseShow(e) {
    console.log(e)
    var dataset = e.currentTarget.dataset;
    if (this.data.orgType[dataset.index].isBusinessCard) {
      this.toggle('orgType[' + dataset.index + '].isBusinessCard', 0)
    } else {
      this.toggle('orgType[' + dataset.index + '].isBusinessCard', 1)
    }

    console.log(this.data.orgType)

  },
  chooseOrg() {
    var that = this
    var arr = []
    this.toggle('userOrgList', this.data.orgType)
    for (var item of that.data.userOrgList) {
      if (item.isBusinessCard) {
        arr.push(item)
      }
    }
    console.log(arr)
    if (arr.length == 1) {
      var isBusinessCard = arr
      that.toggle('isBusinessCard', isBusinessCard)
      that.toggle('isMore', false)
    } else if (arr.length > 1) {
      that.toggle('isMore', true)
      that.toggle('isBusinessCard', [])
    } else {
      that.toggle('isBusinessCard', [])
      that.toggle('isMore', false)
    }
    var orgList = new Array(that.data.userOrgList.length)
    for (var i = 0; i < that.data.userOrgList.length; i++) {
      orgList[i] = {
        id: that.data.userOrgList[i].id,
        isBusinessCard: that.data.userOrgList[i].isBusinessCard
      }
    }
    this.toggle("orgList", orgList)
    this.toggle("typeShow", false)
  },
  // 更换头像
  changeHead: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // that.saveData();
        that.toggle('refresh', false)
        const avatar = res.tempFilePaths[0];
        wx.navigateTo({
          url: '../uploadHead/uploadHead?src=' + avatar
        })
      }
    })
  },
  onInput: function(e) {
    var param = e.currentTarget.dataset.param;
    this.toggle(param, e.detail.value.replace(/^\s+|\s+$/g,''));
  },
  toIpt(e) {
    var type = e.currentTarget.dataset.type;
    this.toggle('iptType', type)
    if (type == "name") {
      this.toggle('nameIpt', true)
    } else if (type == "industry") {
      this.toggle('industryIpt', true)
    } else if (type == "job") {
      this.toggle('jobIpt', true)
    } else if (type == "address") {
      this.toggle('addressIpt', true)
    } else if (type == "email") {
      this.toggle('emailIpt', true)
    } else if (type == "company") {
      this.toggle('company', true)
    }
  },
  hideIpt(e) {
    var content = e.detail.value
    var type = this.data.iptType
    this.toggle(type, content)
    this.toggle(type + 'Ipt', false)
  },
  closeIpt() {
    var type = this.data.iptType
    this.toggle(type + 'Ipt', false)
  },

  save() {
    var that = this;
    app.req({
      url: 'orgmini/user/updateUserInformation',
      data: {
        "realName": that.data.name,
        "sex": that.data.sex,
        "avatarUrl": that.data.avatarUrl,
        "mail": that.data.email,
        "post": that.data.job,
        "industry": that.data.industry,
        "workArea": that.data.address,
        "selfIntroduction": that.data.selfIntroduction,
        "company": that.data.company,
        birthdayDefault: 0,
        isShowBirthday: that.data.isShowBirthday,
        birthday:that.data.birthday
      },
      success: function(res) {
        if (200 == res.code) {
          if (!that.data.orgList) {
            wx.navigateBack()
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          if (!that.data.orgList) {
            wx.navigateBack()
          }
        }
        that.saveOrgInfo();
      }
    });

  },
  saveOrgInfo: function() {
    var that = this;
    if (that.data.orgList) {
      app.req({
        url: 'orgmini/user/updateOrgList',
        data: {
          "orgUserList": that.data.orgList
        },
        success: function(res) {
          if (200 == res.code) {
            wx.navigateBack()
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        }
      });
    } 
  },
  onChange(event) {
    this.setData({
      sex: event.detail
    });
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    this.toggle('refresh', false)
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this

    if (that.data.refresh) {
      var arr = []
      app.getUserInfo(function(userInfo) {
        that.toggle("name", userInfo.realName);
        that.toggle("industry", userInfo.industry);
        that.toggle("job", userInfo.post);
        that.toggle("address", userInfo.workArea);
        that.toggle("phone", userInfo.telphone);
        that.toggle("email", userInfo.mail);
        that.toggle("avatarUrl", userInfo.avatarUrl);
        that.toggle("sex", userInfo.sex.toString());
        that.toggle("selfIntroduction", userInfo.selfIntroduction);
        that.toggle("userOrgList", userInfo.userOrgList);
        that.toggle("orgType", userInfo.userOrgList);
        that.toggle("company", userInfo.company)
        that.toggle("isShowBirthday", userInfo.isShowBirthday)
        that.toggle("isShowBirthday1", userInfo.isShowBirthday)
        that.toggle("birthday", userInfo.birthday)
        that.toggle("year", userInfo.birthday.substring(0, 4))
        that.toggle("month", userInfo.birthday.substring(5, 7))
        that.toggle("day", userInfo.birthday.substring(8, 10))
        for (var item of that.data.userOrgList) {
          if (item.isBusinessCard) {
            arr.push(item)
          }
        }
        if (arr.length == 1) {
          var isBusinessCard = arr
          that.toggle('isBusinessCard', isBusinessCard)
          that.toggle('isMore', false)
        } else if (arr.length > 1) {
          that.toggle('isMore', true)
          that.toggle('isBusinessCard', [])
        } else {
          that.toggle('isBusinessCard', [])
          that.toggle('isMore', false)
        }
      }, true);
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})